<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="活动编号" prop="activityNum">
        <el-input
          v-model="queryParams.activityNum"
          placeholder="请输入活动编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="活动类型" prop="activityType">
        <el-select v-model="queryParams.activityType" placeholder="请选择活动类型" clearable size="small">
          <el-option label="限时抢购" value="1" />
          <el-option label="拼团" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="活动状态" prop="activityStatus">
        <el-select
          v-model="queryParams.activityStatus"
          placeholder="请选择活动状态"
          clearable
          size="small"
        >
          <el-option label="未开始" :value="0" />
          <el-option label="进行中" :value="1" />
          <el-option label="已结束" :value="2" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="活动开始时间" prop="activityStart">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.activityStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择活动开始时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="activityEnd">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.activityEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择活动结束时间"
        ></el-date-picker>
      </el-form-item>-->

      <!-- <el-form-item label="活动商品规格" prop="activityGoodsStandard">
        <el-input
          v-model="queryParams.activityGoodsStandard"
          placeholder="请输入活动商品规格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动商品最低价" prop="activityMinMoney">
        <el-input
          v-model="queryParams.activityMinMoney"
          placeholder="请输入活动商品最低价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动商品最高价" prop="activityMaxMoney">
        <el-input
          v-model="queryParams.activityMaxMoney"
          placeholder="请输入活动商品最高价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="团购文案" prop="groupBuyDesc">
        <el-input
          v-model="queryParams.groupBuyDesc"
          placeholder="请输入团购文案"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="团购人数上限" prop="groupBuyMax">
        <el-input
          v-model="queryParams.groupBuyMax"
          placeholder="请输入团购人数上限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限时抢购折扣模式 1-固定比例 2-折扣比例" prop="panicBuyDiscountModel">
        <el-input
          v-model="queryParams.panicBuyDiscountModel"
          placeholder="请输入限时抢购折扣模式 1-固定比例 2-折扣比例"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入修改人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:activity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:activity:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:activity:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="活动编号"
        width="160"
        align="center"
        prop="activityNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="产品图片" align="center" prop="goodsCover">
        <template slot-scope="scope">
          <el-image
            style="width:40px;height:40px"
            fit="cover"
            :src="scope.row.goods.goodsCover[0]"
            :preview-src-list="scope.row.goods.goodsCover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="产品名称"
        align="center"
        prop="goods.goodsName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="产品价格" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.goods.goodsMoneyMin}}元~{{scope.row.goods.goodsMoneyMax}}元</span>
        </template>
      </el-table-column>
      <el-table-column label="活动类型" align="center" prop="activityType">
        <template slot-scope="scope">
          <span>{{ scope.row.activityType | filterActivityType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动开始时间" align="center" prop="activityStart" width="160" />
      <el-table-column label="活动结束时间" align="center" prop="activityEnd" width="160" />
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.activityStatus | filterActivityStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.activityStatus !== 2"
            size="mini"
            type="text"
            @click="finishActivity(scope.row)"
            v-hasPermi="['system:activity:edit']"
          >结束活动</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:activity:edit']"
          >编辑</el-button>
          <el-button
            v-if="scope.row.activityStatus ===2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="delete-btn"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:activity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营销对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动编号" prop="activityNum">
          <el-input v-model="form.activityNum" placeholder="请输入活动编号" />
        </el-form-item>
        <el-form-item label="活动类型 1-限时抢购,2-拼团" prop="activityType">
          <el-select v-model="form.activityType" placeholder="请选择活动类型 1-限时抢购,2-拼团">
            <el-option label="请选择字典生成" value />
          </el-select>
        </el-form-item>
        <el-form-item label="活动开始时间" prop="activityStart">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.activityStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择活动开始时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="activityEnd">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.activityEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择活动结束时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="活动状态">
          <el-radio-group v-model="form.activityStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="活动商品规格" prop="activityGoodsStandard">
          <el-input v-model="form.activityGoodsStandard" placeholder="请输入活动商品规格" />
        </el-form-item>
        <el-form-item label="活动商品最低价" prop="activityMinMoney">
          <el-input v-model="form.activityMinMoney" placeholder="请输入活动商品最低价" />
        </el-form-item>
        <el-form-item label="活动商品最高价" prop="activityMaxMoney">
          <el-input v-model="form.activityMaxMoney" placeholder="请输入活动商品最高价" />
        </el-form-item>
        <el-form-item label="团购文案" prop="groupBuyDesc">
          <el-input v-model="form.groupBuyDesc" placeholder="请输入团购文案" />
        </el-form-item>
        <el-form-item label="团购人数上限" prop="groupBuyMax">
          <el-input v-model="form.groupBuyMax" placeholder="请输入团购人数上限" />
        </el-form-item>
        <el-form-item label="限时抢购折扣模式 1-固定比例 2-折扣比例" prop="panicBuyDiscountModel">
          <el-input v-model="form.panicBuyDiscountModel" placeholder="请输入限时抢购折扣模式 1-固定比例 2-折扣比例" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="修改人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入修改人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listActivity,
  getActivity,
  delActivity,
  addActivity,
  updateActivity,
  exportActivity
} from "@/api/system/activity";

export default {
  name: "Activity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      activityNums: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营销表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityNum: null,
        activityType: null,
        activityStart: null,
        activityEnd: null,
        activityStatus: null,
        activityGoodsStandard: null,
        activityMinMoney: null,
        activityMaxMoney: null,
        groupBuyDesc: null,
        groupBuyMax: null,
        panicBuyDiscountModel: null,
        goodsId: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  filters: {
    filterActivityType(value) {
      return {
        1: "限时抢购",
        2: "拼团"
      }[value];
    },
    filterActivityStatus(value) {
      return {
        0: "未开始",
        1: "进行中",
        2: "已结束"
      }[value];
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询营销列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows.map(item => {
          item.goods.goodsCover = item.goods.goodsCover.split(",");
          return item
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        activityNum: null,
        activityType: null,
        activityStart: null,
        activityEnd: null,
        activityStatus: 0,
        activityGoodsStandard: null,
        activityMinMoney: null,
        activityMaxMoney: null,
        groupBuyDesc: null,
        groupBuyMax: null,
        panicBuyDiscountModel: null,
        goodsId: null,
        userId: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.activityNums = selection.map(item => item.activityNum);

      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "ActivityEdit"
      });
    },
    // 结束活动
    finishActivity(row) {
      updateActivity({
        id: row.id,
        goodsId: row.goodsId,
        activityStatus: 2,
        activityStart: row.activityStart,
        activityEnd: row.activityEnd
      }).then(response => {
        if (response.code === 200) {
          this.msgSuccess("操作成功");
          this.getList();
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "ActivityEdit",
        query: {
          id: row.id
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const activityNums = row.activityNum || this.activityNums;
      this.$confirm(
        '是否确认删除活动编号为"' + activityNums + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delActivity(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有营销数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportActivity(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
